﻿@model LogManager.ViewModels.ErrorIndexViewModel
@{
    ViewBag.Title = "Error Center";
    ViewBag.Breadcrumb = "Error Center";
}

@section AsideLeft{
    @Html.Partial("ErrorNavigation")
}

<h2>@ViewBag.Title</h2>
<div id="errors" class="group">
    @using (Html.BeginForm())
     {
        <fieldset class="filter">
            <legend>
                <h3>Event Source Filter</h3>
            </legend>
            @Html.DisplayFor(m => m.SourceFilter)
        </fieldset>
        
        <fieldset class="filter">        
            <legend>
                <h3>Exception Type Filter</h3>
            </legend>
            @Html.DisplayFor(m => m.ExceptionFilter)
        </fieldset>
        
        <br clear="all" />
        <div>
            <input id="doFilter" type="submit" value="Filter" />
        </div>  
     }
    
    <fieldset>
        <legend><h3>Errors</h3></legend>
        <div id="load-placeholder">
            <p id="errors-placeholder">
                <img src="@Url.Content("~/Content/Images/ajax-loader.gif")" alt="Loading" />
            </p>
        </div>
    </fieldset>
</div>

@section Foot{
<script type="text/javascript">
    $(document).ready(function () {
        $('#errors-placeholder').load('@(Url.Action("List", "Error"))');

        var serverName = $('#SourceFilter_ServerName');
        var logName = $('#SourceFilter_LogName');

        serverName.change(function () {
            logName.html('');

            if (serverName.val() === 'All') {
                var val = 'All';
                logName.append('<option value="' + val + '" >' + val + '</option>');
            }
            else if (serverName.val() !== null) {
                $.getJSON('@(Url.Action("LogNamesData","Filter"))' + '?serverName=' + serverName.val(), function (data) {
                    $.each(data, function (key, val) {
                        logName.append('<option value="' + val + '" >' + val + '</option>');
                    });
                });
            }
        });

        $('#doFilter').click(function (e) {
            e.preventDefault();
            //show loader
            $('#load-placeholder').html('<p id="errors-placeholder"><img src="@Url.Content("~/Content/Images/ajax-loader.gif")" alt="Loading" /></p>');

            var model = {
                logName: $('#SourceFilter_LogName').val(),
                serverName: $('#SourceFilter_ServerName').val(),
                exception: $('#ExceptionFilter_ExceptionType').val()
            };

            for (var prop in model) {
                if (model[prop] === "All" || model[prop] === null || model[prop] === "" || model[prop] == "Undefined") {
                    delete model[prop];
                }
            }

            $.get('@(Url.Action("List","Error"))' + '?' + $.param(model), function (html) {
                $('#load-placeholder').html(html);
            });
        });
    });
</script>
}